use "\oa_2017_abvfinalWP.dta" 

set more off

*--------------------------DVs-------------------------------------------
*Question 1: IMSS v. Seguro Popular
gen health_tri_op=ab1
recode health_tri_op 4=. 2=3 3=2
label define health_tri 1 "IMSS (contrib)" 3 "Seguro Popular (noncontrib)" 2 "No aumente (no increase)"
label values  health_tri_op health_tri
label variable health_tri_op "Health insurance: trichotomy"

gen health_ord_op=.
replace health_ord_op=0 if ab1==1 & ab_1_1==1
replace health_ord_op=1 if ab1==1 & ab_1_1==2
replace health_ord_op=1 if ab1==1 & ab_1_1==4
replace health_ord_op=2 if ab1==1 & ab_1_1==3
replace health_ord_op=3 if ab1==3 
replace health_ord_op=4 if ab1==2 & ab_1_1==3
replace health_ord_op=5 if ab1==2 & ab_1_1==2
replace health_ord_op=5 if ab1==2 & ab_1_1==4
replace health_ord_op=6 if ab1==2 & ab_1_1==1
label define health_ord 0 "IMSS (contrib): very favorable" 1 "IMSS (contrib): somewhat favorable" 2 "IMSS (contrib): a little favorable" ///
3 "No aumente (no increase)" 4 "Seguro Popular (noncontrib): a little favorable" 5 "Seguro Popular (noncontrib): somewhat favorable" ///
6 "Seguro Popular (noncontrib): very favorable" 
label values health_ord_op health_ord
label variable health_ord_op "Health insurance: ordinal"

*Question 2: IMSS v. Programa de Pension para Adultos Mayores
gen pension_tri_op=ab_2
recode pension_tri_op 4=. 2=3 3=2
label define pension_tri 1 "IMSS (contrib)" 3 "Pension Adultos Mayores (noncontrib)" 2 "No aumente (no increase)"
label values  pension_tri_op pension_tri
label variable pension_tri_op "Pension: trichotomy"

gen pension_ord_op=.
replace pension_ord_op=0 if ab_2==1 & ab_2_1==1
replace pension_ord_op=1 if ab_2==1 & ab_2_1==2
replace pension_ord_op=1 if ab_2==1 & ab_2_1==4
replace pension_ord_op=2 if ab_2==1 & ab_2_1==3
replace pension_ord_op=3 if ab_2==3 
replace pension_ord_op=4 if ab_2==2 & ab_2_1==3
replace pension_ord_op=5 if ab_2==2 & ab_2_1==2
replace pension_ord_op=5 if ab_2==2 & ab_2_1==4
replace pension_ord_op=6 if ab_2==2 & ab_2_1==1
label define pension_ord 0 "IMSS (contrib): very favorable" 1 "IMSS (contrib): somewhat favorable" 2 "IMSS (contrib): a little favorable" ///
3 "No aumente (no increase)" 4 "Pension Adultos Mayores (noncontrib): a little favorable" 5 "Pension Adultos Mayores (noncontrib): somewhat favorable" ///
6 "Pension Adultos Mayores (noncontrib): very favorable" 
label values pension_ord_op pension_ord
label variable pension_ord_op "Pension: ordinal"

	*FIGURE 4: CROSSTAB USED TO MAKE BUBBLE PLOT
tab health_tri pension_tri, cel

*Gen Contrib v noncontrib
egen temp=rowmean(pension_ord_op health_ord_op)
egen contrib_v_noncontrib=std(temp)

*--------------------------SIMPLE IVs-------------------------------------------
	*----------Current labor market situation-----------------------
*Create labormarket_current variable
gen labormarket_current=.
		*page 25 of WB2007: at less than 10 workers in your firm, you are almost certainly informal. 
	*informal SE: own account worker and no pension debit
replace labormarket_current=10 if q_162~=1 & ab_9a_1==1 & ab_9==1 
	*formal wage earner (benefits definition)
replace labormarket_current=8 if q_162==1 & ab_9a_1==2 & ab_9==1
	*informal wage earner (benefits definition)
replace labormarket_current=9 if q_162>1 & ab_9a_1==2 & ab_9==1
	*trabajo sin pago is informal WE
replace labormarket_current = 9 if ab_9a_1==3 & ab_9==1
	*the above codes all but 14 active workers. 13 are own-account but claim they have a pension debit.  I code these as formal WE in the next line
replace labormarket_current = 8 if q_162==1 & ab_9a_1==1 & ab_9==1	
	*unemployed
replace labormarket_current = 13 if ab_9a_2==1 & ab_9==2
	*retired or disabled
replace labormarket_current = 6 if ab_9a_2==2 & ab_9==2
	*housework
replace labormarket_current = 5 if ab_9a_2==3 & ab_9==2
	*student
replace labormarket_current = 4 if ab_9a_2==4 & ab_9==2
	*other non-eap
replace labormarket_current = 7 if ab_9a_2==5
	*the above leaves just one person missing.
label define labormarket 13 Unemployed 4 Student 5 Homemaker 6 "Retired or disabled" 7 "Other non-EAP" 8 "Formal WE" 9 "Informal WE" 10 "Informal SE" 11 "Formal Employer"
label values labormarket_current labormarket

label define twogroups 13 "Unemployed" 8 "Formal Wage Earner" 9 "Informal" 12 "Non-EAP"
*recode labormarket (3=13) (4=12) (5=12) (6=12) (7=12) (8=8) (9=9) (10=9) (11=11), gen(threegroups)
*label values threegroups threegroups 

recode labormarket_current (3=13) (4=12) (5=12) (6=12) (7=12) (8=8) (9=9) (10=9) (11=8), gen(twogroups_current)
label values twogroups_current twogroups

recode twogroups_current 9=1 .=. else=0, gen(informal_current)
label variable informal_current "Informal worker"

recode twogroups_current 13=1 .=. else=0, gen(unemployed_current)
label variable unemployed_current "Unemployed"

recode twogroups_current 12=1 .=. else=0, gen(noneap_current)
label variable noneap_current "Non-EAP"

*Create continuous informality measure
recode q_160 2=1 1=0 3=., gen(contract)
recode q_158 1/2=2 3=1 4/10=0 11=., gen(size)
recode q_162 1=0 2/3=1 4=., gen(benefits)
	*TABLE OA.4
irt grm contract size benefits, intpoints(20) difficult 

predict informal_irt_current if ab_9==1, latent 
summ informal_irt_current
replace informal_irt_current=(informal_irt_current-r(min))/(r(max)-r(min))
recode informal_irt_current .=0
label variable informal_irt_current "Informal (continuous)"

	*----------Current and (if non-EAP) past labor market situation-----------------------	
*Create labormarket_past variable
gen labormarket_past =.
		*page 25 of WB2007: at less than 10 workers in your firm, you are almost certainly informal. 
	*informal SE: own account worker and no pension debit
replace labormarket_past=10 if q_162~=1 & ab_9a_1==1 & ab_9==1 
	*formal wage earner (benefits definition)
replace labormarket_past=8 if q_162==1 & ab_9a_1==2 & ab_9==1
	*informal wage earner (benefits definition)
replace labormarket_past=9 if q_162>1 & ab_9a_1==2 & ab_9==1
	*trabajo sin pago is informal WE
replace labormarket_past= 9 if ab_9a_1==3 & ab_9==1
	*the above codes all but 14 active workers. 13 are own-account but claim they have a pension debit.  I code these as formal WE in the next line
replace labormarket_past= 8 if q_162==1 & ab_9a_1==1 & ab_9==1	
*now, work on most recent job for unemployed, retired, and homemakers
	*formal wage earner (benefits definition)
replace labormarket_past= 8 if q_163==1 & q_157~=4 & ab_9==2 & labormarket_current==5 /*currently homemaker*/  
replace labormarket_past= 8 if q_163==1 & q_157~=4 & ab_9==2 & labormarket_current==6 /*currently retired*/
replace labormarket_past= 8 if q_163==1 & q_157~=4 & ab_9==2 & labormarket_current==7 /*currently other non-EAP*/
replace labormarket_past= 8 if q_163==1 & q_157~=4 & ab_9==2 & labormarket_current==13 /*unemployed*/

	*informal wage earner (benefits definition)
replace labormarket_past= 9 if q_163>1 & q_163<4 & q_157==1 & ab_9==2 & labormarket_current==5 /*currently homemaker*/  
replace labormarket_past= 9 if q_163>1 & q_163<4 & q_157==1 & ab_9==2 & labormarket_current==6  /*currently retired*/
replace labormarket_past= 9 if q_163>1 & q_163<4 & q_157==1 & ab_9==2 & labormarket_current==7 /*currently other non-EAP*/
replace labormarket_past= 9 if q_163>1 & q_163<4 & q_157==1 & ab_9==2 & labormarket_current==13 /*unemployed*/
		*informal wage earner from sin pago 
replace labormarket_past= 9 if q_157==3 & ab_9==2 & labormarket_current==5 /*currently homemaker*/  
replace labormarket_past= 9 if q_157==3 & ab_9==2 & labormarket_current==6  /*currently retired*/
replace labormarket_past= 9 if q_157==3 & ab_9==2 & labormarket_current==7 /*currently other non-EAP*/
replace labormarket_past= 9 if q_157==3 & ab_9==2 & labormarket_current==13 /*unemployed*/

	*informal SE (benefits definition)
replace labormarket_past= 10 if q_163>1 & q_163<4 & q_157==2 & ab_9==2 & labormarket_current==5 /*currently homemaker*/  
replace labormarket_past= 10 if q_163>1 & q_163<4 & q_157==2 & ab_9==2 & labormarket_current==6  /*currently retired*/
replace labormarket_past= 10 if q_163>1 & q_163<4 & q_157==2 & ab_9==2 & labormarket_current==7 /*currently other non-EAP*/
replace labormarket_past= 10 if q_163>1 & q_163<4 & q_157==2 & ab_9==2 & labormarket_current==13 /*unemployed*/

	*never in the EAP
replace labormarket_past=7 if q_157==4 & ab_9==2  

	*Student
replace labormarket_past=4 if labormarket_current==4 & ab_9==2  

	*this leaves 10 people missing, all non-EAP. Too many ns/nc to score them.
label define labormarket2 13 Unemployed 4 Student 5 Homemaker 6 "Retired or disabled" 7 "Non-student, never EAP" 8 "Formal WE" 9 "Informal WE" 10 "Informal SE" 11 "Formal Employer"
label values labormarket_past labormarket2 

recode labormarket_past (3=13) (4=12) (5=12) (6=12) (7=12) (8=8) (9=9) (10=9) (11=8), gen(twogroups_past)
label values twogroups_past twogroups

recode twogroups_past 9=1 .=. else=0, gen(informal_past)
label variable informal_past "Informal worker"

recode twogroups_past 13=1 .=. else=0, gen(unemployed_past)
label variable unemployed_past "Unemployed"

recode twogroups_past 12=1 .=. else=0, gen(noneap_past)
label variable noneap_past "Non-EAP"

	*Here is distribution of roles among current non-EAP
tab labormarket_current if labormarket_current<8 | labormarket_current>11

	*Here is distribution of formal/informal status in most recent job among current non-EAP
tab labormarket_past if labormarket_current<8 | labormarket_current>11


*Create continuous informality measure
gen contract_past=contract 
recode q_161 2=1 1=0 3=., gen(contract_temp)
replace contract_past= contract_temp if ab_9==2

gen size_past=size 
recode q_159 1/2=2 3=1 4/10=0 11=., gen(size_temp)
replace size_past= size_temp if ab_9==2

gen benefits_past=benefits
recode q_163 1=0 2/3=1 4=., gen(benefits_temp)
replace benefits_past=benefits_temp if ab_9==2

irt grm contract_past size_past benefits_past, intpoints(20) difficult 
predict informal_irt_past if q_157==1 | q_157==2 | q_157==3 | q_157==. , latent 
summ informal_irt_past
replace informal_irt_past=(informal_irt_past-r(min))/(r(max)-r(min))
recode informal_irt_past .=0
label variable informal_irt_past "Informal (continuous)"

*----------Demographics-----------------------
recode t_172_1- t_172_16 (2=0) (1=1) (3=.)
recode q_19 (2=0) (1=1) (3=.)
irt grm t_172_1- t_172_16 q_19 q_174
predict wealth_temp, latent
egen wealth=std(wealth_temp)

gen lneduc=ln(q_153+1)
label variable lneduc "Education level"

gen lnage=ln(q_11)
label variable lnage "Age"

rename q_10 woman
recode woman 2=1 1=0
label drop q_10
label variable woman "Woman"

summ q_153 q_11 woman

*-------------------------------MODELS WITHOUT REVISIONIST VIEW-----------------------------------
eststo clear
	*TABLE 3
			*HEALTH
*Next: Ordinal--Binary
*Model 1.1
eststo: ologit health_ord_op informal_current noneap_current unemployed_current lneduc lnage woman 
display 2.73/(2.73^2+(e(N)-e(k)))^.5
margins, dydx(informal_current) at(noneap_current==0 unemployed_current==0 lneduc lnage woman) 
margins, dydx(informal_current) at(noneap_current==0 unemployed_current==0 lneduc lnage woman) predict(xb)
summ  health_ord_op
display .4434835 /r(Var)^.5

*Next: Ord--Continuous
*Model 1.2
eststo: ologit health_ord_op informal_irt_current noneap_current unemployed_current lneduc lnage woman 
display 2.83/(2.83^2+(e(N)-e(k)))^.5
margin, at((min) informal_irt_current noneap_current=0 unemployed_current=0) at((max) informal_irt_current noneap_current=0 unemployed_current=0) atmeans contrast(atcontrast(r))
quietly sum informal_irt_current
local range=r(max)-r(min)
display `range'
quietly sum health_ord_op
display (.5237518*`range')/r(Var)^.5

			*PENSION
*Next: Ordinal--Binary
*Model 1.3
eststo: ologit pension_ord_op informal_current noneap_current unemployed_current lneduc lnage woman 
display 2.78/(2.78^2+(e(N)-e(k)))^.5
margin, at(informal_current=0) at(informal_current=1) atmeans contrast(atcontrast(r))
margin, at(informal_current=0) at(informal_current=1) atmeans predict(xb) contrast(atcontrast(r))
summ  pension_ord_op
display .4398686/r(Var)^.5

*Next: Ord--Continuous
*Model 1.4
eststo: ologit pension_ord_op informal_irt_current noneap_current unemployed_current lneduc lnage woman 
display 3.13/(3.13^2+(e(N)-e(k)))^.5
margin, at((min) informal_irt_current noneap_current=0 unemployed_current=0) at((max) informal_irt_current noneap_current=0 unemployed_current=0) atmeans contrast(atcontrast(r))
quietly sum informal_irt_current
local range=r(max)-r(min)
display `range'
quietly sum pension_ord_op
display (.562357*`range')/r(Var)^.5

*----------
*Most powered: Continuous--Bin
*Model 1.5
eststo: reg contrib_v_noncontrib informal_current noneap_current unemployed_current lneduc lnage woman
display 3.37/(3.37^2+(e(N)-e(rank)))^.5
margin, at(informal_current=0) at(informal_current=1) atmeans contrast(atcontrast(r))

*Model 1.6
eststo: reg contrib_v_noncontrib informal_irt_current noneap_current unemployed_current lneduc lnage woman
display 3.78/(3.78^2+(e(N)-e(rank)))^.5
margin, at((min) informal_irt_current noneap_current=0 unemployed_current=0) at((max) informal_irt_current noneap_current=0 unemployed_current=0) atmeans contrast(atcontrast(r))

esttab est1 est2 est3 est4 est5 est6 using Table1.rtf, label b(3) replace se star(* 0.05 ) nogap onecell title(Table 1:)

*-------------------------------PAST: MODELS USING MOST RECENT JOB FOR CURRENT NON-EAP AND UNEMPLOYED-----------------------------------
eststo clear
	*TABLE 4
			*HEALTH
*Next: Ordinal--Binary
*Model 2.1
eststo: ologit health_ord_op informal_past noneap_past  lneduc lnage woman 
display 4.93/(4.93^2+(e(N)-e(k)))^.5
margins, dydx(informal_past) at(noneap_past==0 lneduc lnage woman) 
margins, dydx(informal_past) at(noneap_past==0 lneduc lnage woman) predict(xb)
summ  health_ord_op
display  .6407647 /r(Var)^.5

*Next: Ord--Continuous
*Model 2.2
eststo: ologit health_ord_op informal_irt_past noneap_past  lneduc lnage woman 
display 3.74/(3.74^2+(e(N)-e(k)))^.5
margin, at((min) informal_irt_past noneap_past=0 ) at((max) informal_irt_past noneap_past=0 ) atmeans contrast(atcontrast(r))
quietly sum informal_irt_past
local range=r(max)-r(min)
display `range'
quietly sum health_ord_op
display (.5447247*`range')/r(Var)^.5

			*PENSION
*Next: Ordinal--Binary
*Model 2.3
eststo: ologit pension_ord_op informal_past noneap_past lneduc lnage woman 
display 3.68/(3.68^2+(e(N)-e(k)))^.5
margin, at(informal_past=0) at(informal_past=1) atmeans contrast(atcontrast(r))
margin, at(informal_past=0) at(informal_past=1) atmeans predict(xb) contrast(atcontrast(r))
summ  pension_ord_op
display .4676198/r(Var)^.5

*Next: Ord--Continuous
*Model 2.4
eststo: ologit pension_ord_op informal_irt_past noneap_past lneduc lnage woman 
display 4.23/(4.23^2+(e(N)-e(k)))^.5
margin, at((min) informal_irt_past noneap_past=0 ) at((max) informal_irt_past noneap_past=0 ) atmeans contrast(atcontrast(r))
quietly sum informal_irt_past
local range=r(max)-r(min)
display `range'
quietly sum pension_ord_op
display (.6047483 *`range')/r(Var)^.5

			*Index
*Most powered: Continuous--Bin
*Model 2.5
eststo: reg contrib_v_noncontrib informal_past noneap_past lneduc lnage woman
display 5.54/(5.54^2+(e(N)-e(rank)))^.5
margin, at(informal_past=0) at(informal_past=1) atmeans contrast(atcontrast(r))

*Model 2.6
eststo: reg contrib_v_noncontrib informal_irt_past noneap_past lneduc lnage woman
display 5.29/(5.29^2+(e(N)-e(rank)))^.5
margin, at((min) informal_irt_past noneap_past=0) at((max) informal_irt_past noneap_past=0) atmeans contrast(atcontrast(r))

esttab est1 est2 est3 est4 est5 est6 using Table2.rtf, label b(3) replace se star(* 0.05 ) nogap onecell title(Table 1:)
eststo clear

*-------------------------------REVISIONIST IVS AND MODELS-----------------------------------
	*-------------------------------SPOUSES-----------------------------------
recode q_167 (1=1) (2=0) (3=0) (6=0) (5=0) (7=.), gen(spouse_formal)
recode q_167 (1=0) (2=0) (3=0) (6=1) (5=1) (7=.), gen(spouse_nonworking)
gen gets_bennies_spouse=spouse_formal
replace gets_bennies_spouse=0 if twogroups_current==8

	*------------------------------LABOR MARKET EXPECTATIONS-----------------------------------
*Create histogram 
*Create labormarket_current_expectations 
gen labormarket_expectations_current=1 if twogroups_current==8 & q_165==4
replace labormarket_expectations_current=2 if twogroups_current==8 & q_165==3
replace labormarket_expectations_current=3 if twogroups_current==8 & q_165==2
replace labormarket_expectations_current=4 if twogroups_current==8 & q_165==1
replace labormarket_expectations_current=5 if twogroups_current==9 & q_166==1
replace labormarket_expectations_current=6 if twogroups_current==9 & q_166==2
replace labormarket_expectations_current=7 if twogroups_current==9 & q_166==3
replace labormarket_expectations_current=8 if twogroups_current==9 & q_166==4
label define expect 1 "Formal: transition is nada probable" 2 "Formal: transition is poco probable" 3 "Formal: transition is bastante probable" 4 "Formal: transition is muy probable" 5 "Informal: transition is muy probable" 6 "Informal: transition is bastante probable" 7 "Informal: transition is poco probable" 8 "Informal: transition is nada probable"
label values labormarket_expectations_current expect 

bysort labormarket_expectations_current: summ wealth
xtile wealth_pct=wealth, nq(100)
bysort labormarket_expectations_current: summ wealth_pct

 
recode q_167 (1=1) (2=3) (3=3) (5=2) (6=2) (7=.), gen(spouse)

label define spouse 1 "Formal Spouse"  2 "Non-working or no spouse" 3 "Informal spouse"
label values spouse spouse
label variable spouse "Spouse's Situation"

recode q_167 (1=1) (2=4) (3=4) (5=2) (6=3) (7=.), gen(spouse_new)

label define spouse_new 1 "Formal Spouse"  2 "No spouse" 3 "Nonworking spouse" 4 "Informal spouse"
label values spouse_new spouse_new
label variable spouse_new "Spouse's Situation"


gen spouse_not_formal=0
replace spouse_not_formal=1 if twogroups_current==8 & q_167==2 
replace spouse_not_formal=1 if twogroups_current==8 & q_167==3
replace spouse_not_formal=1 if twogroups_current==8 & q_167==6 

gen spouse_not_formal_all=0
replace spouse_not_formal_all=1 if q_167==2 
replace spouse_not_formal_all=1 if q_167==3
replace spouse_not_formal_all=1 if q_167==6 

gen spouse_is_formal=0
replace spouse_is_formal=1 if twogroups_current==8 & q_167==1 


histogram labormarket_expectations_current, discrete fraction width(.5) color(black) xlab(2.5 "Subjective prob. of a transition to informality" 6.5 "Subjective prob. of a transition to formality", notick labsize(small) labgap(4)) ///
xmlab(1 "Zero" 2 "Low" 3 "Medium" 4 "High" 5 "High" 6 "Medium" 7 "Low" 8 "Zero", labsize(vsmall)) ///
ytitle("Proportion") ///
xscale(range(.75 8.25)) xline(4.5, lcolor(grey) lwidth(thin)) ///
legend(off) xtitle("") graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) ///
text(.29 2.5 "Formal Workers") text(.29 6.5 "Informal Workers")

	*TABLE 2
bysort labormarket_expectations_current: summ wealth q_153 q_11 woman 

tab labormarket_expectations_current spouse_new if spouse_new~=2, row
recode labormarket_expectations_current 1/2=1 3/4=3 5/6=5 7/8=7, gen(temp2)
tab temp2 spouse_new if spouse_new~=2, row

	*FIGURE 2
*no legend
twoway histogram labormarket_expectations_current, discrete freq width(.5) color(black) xlab(2.5 "{bf:Prob. of transition to informality}" 6.5 "{bf:Prob. of transition to formality}", notick labsize(small) labgap(4)) ///
xmlab(1 "Zero" 2 "Low" 3 "Medium" 4 "High" 5 "High" 6 "Medium" 7 "Low" 8 "Zero", labsize(small)) ///
ytitle("{bf:Proportion of the employed labor force}") ///
xscale(range(.75 8.25)) xline(4.5, lcolor(grey) lwidth(thin)) ylab(0 "0" 32 ".05" 64 ".10" 96 ".15" 128 ".20" 160 ".25" 192 ".30") ///
xtitle("") graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) ///
text(187 2.5 "{bf:Formal Workers}") text(187 6.5 "{bf:Informal workers}") ///
legend(off) || ///
histogram labormarket_expectations_current if spouse_new==1 | spouse_new==2 | spouse_new==3, discrete freq width(.5) color(gs4)   || ///
histogram labormarket_expectations_current if spouse_new==1 | spouse_new==2, discrete freq width(.5) color(gs10) || ///
histogram labormarket_expectations_current if spouse_new==1, discrete freq width(.5) color(gs14) 

*with legend
twoway histogram labormarket_expectations_current, discrete freq width(.5) color(black) xlab(2.5 "{bf:Prob. of transition to informality}" 6.5 "{bf:Prob. of transition to formality}", notick labsize(small) labgap(4)) ///
xmlab(1 "Zero" 2 "Low" 3 "Medium" 4 "High" 5 "High" 6 "Medium" 7 "Low" 8 "Zero", labsize(small)) ///
ytitle("{bf:Proportion of the employed labor force}") ///
xscale(range(.75 8.25)) xline(4.5, lcolor(grey) lwidth(thin)) ylab(0 "0" 32 ".05" 64 ".10" 96 ".15" 128 ".20" 160 ".25" 192 ".30") ///
xtitle("") graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) ///
text(187 2.5 "{bf:Formal Workers}") text(187 6.5 "{bf:Informal workers}") ///
legend(order(1 "Informal spouse" 2 "Nonworking spouse" 3 "No spouse" 4 "Formal spouse") cols(1) pos(3) ring(100) size(small)) || ///
histogram labormarket_expectations_current if spouse_new==1 | spouse_new==2 | spouse_new==3, discrete freq width(.5) color(gs4)   || ///
histogram labormarket_expectations_current if spouse_new==1 | spouse_new==2, discrete freq width(.5) color(gs10) || ///
histogram labormarket_expectations_current if spouse_new==1, discrete freq width(.5) color(gs14) 


			*-----figure of whole electorate
recode labormarket_expectations_current 5=6 6=7 7=8 8=9, gen(labormarket_expec_current_fig) 
replace labormarket_expec_current_fig=5 if noneap_current==1
replace labormarket_expec_current_fig=5 if unemployed_current==1

twoway histogram labormarket_expec_current_fig, discrete freq width(.5) color(black) xlab(2.5 "{bf:Prob. of transition to informality}" 6.5 "{bf:Prob. of transition to formality}", notick labsize(small) labgap(4)) ///
xmlab(1 "Zero" 2 "Low" 3 "Medium" 4 "High" 6 "High" 7 "Medium" 8 "Low" 9 "Zero", labsize(small)) ///
ytitle("{bf:Proportion of the adult population}") ///
xscale(range(.75 8.25)) xline(4.5, lcolor(grey) lwidth(thin))  ///
xtitle("") graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) ///
text(187 2.5 "{bf:Formal Workers*}") text(187 6.5 "{bf:Informal workers*}") ///
legend(order(1 "Informal spouse" 2 "No or non-working spouse" 3 "Formal spouse") cols(1) pos(0) ring(100) size(small)) || ///
histogram labormarket_expec_current_fig if spouse==1 | spouse==2, discrete freq width(.5) color(gs8) || ///
histogram labormarket_expec_current_fig if spouse==1, discrete freq width(.5) color(gs12) 
	*note that you have to move the legend manually in the graph editor


*Create labormarket_past_expectations 
gen labormarket_expectations_past=1 if labormarket_past==8 & q_165==4
replace labormarket_expectations_past=2 if labormarket_past==8 & q_165==3
replace labormarket_expectations_past=3 if labormarket_past==8 & q_165==2
replace labormarket_expectations_past=4 if labormarket_past==8 & q_165==1
replace labormarket_expectations_past=5 if labormarket_past>8 & labormarket_past<11 & q_166==1
replace labormarket_expectations_past=6 if labormarket_past>8 & labormarket_past<11 & q_166==2
replace labormarket_expectations_past=7 if labormarket_past>8 & labormarket_past<11 & q_166==3
replace labormarket_expectations_past=8 if labormarket_past>8 & labormarket_past<11 & q_166==4
label values labormarket_expectations_past expect 
 
histogram labormarket_expectations_past, discrete fraction width(.5) color(black) xlab(2.5 "Subjective prob. of a transition to informality" 6.5 "Subjective prob. of a transition to formality", notick labsize(small) labgap(4)) ///
xmlab(1 "Zero" 2 "Low" 3 "Medium" 4 "High" 5 "High" 6 "Medium" 7 "Low" 8 "Zero", labsize(vsmall)) ///
ytitle("Proportion") ///
xscale(range(.75 8.25)) xline(4.5, lcolor(grey) lwidth(thin)) ///
legend(off) xtitle("") graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) ///
text(.29 2.5 "Formal Workers") text(.29 6.5 "Informal Workers")

twoway histogram labormarket_expectations_past, discrete freq width(.5) color(black) xlab(2.5 "{bf:Prob. of transition to informality}" 6.5 "{bf:Prob. of transition to formality}", notick labsize(small) labgap(4)) ///
xmlab(1 "Zero" 2 "Low" 3 "Medium" 4 "High" 5 "High" 6 "Medium" 7 "Low" 8 "Zero", labsize(small)) ///
ytitle("{bf:Proportion of the employed labor force}") ///
xscale(range(.75 8.25)) xline(4.5, lcolor(grey) lwidth(thin)) ylab(0 "0" 32 ".05" 64 ".10" 96 ".15" 128 ".20" 160 ".25" 192 ".30") ///
xtitle("") graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) ///
text(187 2.5 "{bf:Formal Workers}") text(187 6.5 "{bf:Informal workers}") ///
legend(order(1 "Informal spouse" 2 "No or non-working spouse" 3 "Formal spouse") cols(1) pos(0) ring(100) size(small)) || ///
histogram labormarket_expectations_past if spouse==1 | spouse==2, discrete freq width(.5) color(gs8) || ///
histogram labormarket_expectations_past if spouse==1, discrete freq width(.5) color(gs12) 
	*note that you have to move the legend manually in the graph editor

recode labormarket_expectations_past 5=6 6=7 7=8 8=9, gen(labormarket_expec_past_fig) 
replace labormarket_expec_past_fig=5 if noneap_past==1
replace labormarket_expec_past_fig=. if labormarket_current==6

			*-----FIRURE OA.2 of whole electorate
twoway histogram labormarket_expec_past_fig, discrete freq width(.5) color(black) xlab(2.5 "{bf:Prob. of transition to informality}" 7.5 "{bf:Prob. of transition to formality}", notick labsize(small) labgap(4.5)) ///
xmlab(1 "Zero" 2 "Low" 3 "Medium" 4 "High" 6 "High" 7 "Medium" 8 "Low" 9 "Zero", labsize(small)) ///
ytitle("{bf:Proportion of the adult population}") ///
xscale(range(.75 8.25)) xline(4.5, lcolor(grey) lwidth(thin)) xline(5.5, lcolor(grey) lwidth(thin))  ylab(0 "0" 55 ".05" 110 ".10" 165 ".15" 220 ".20" 275 ".25") ///
xtitle("") graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) ///
text(270 2.5 "{bf:Formal Workers*}") text(270 7.5 "{bf:Informal workers*}") text(266 5 "{bf:Never}" "{bf:EAP}")  ///
legend(off) || ///
histogram labormarket_expec_past_fig if spouse_new==1 | spouse_new==2 | spouse_new==3, discrete freq width(.5) color(gs4)   || ///
histogram labormarket_expec_past_fig if spouse_new==1 | spouse_new==2, discrete freq width(.5) color(gs10) || ///
histogram labormarket_expec_past_fig if spouse_new==1, discrete freq width(.5) color(gs14) 
	*note that you have to move the legend manually in the graph editor
 
*notice this change, right before doing the regressions. Recoding noneap_past individuals to be 1 on the labormarket expectations variables.  
recode  labormarket_expectations_past .=1 if noneap_past~=.
recode labormarket_expectations_current .=1 if noneap_current~=. & unemployed_current~=.

recode  labormarket_expectations_past .=1  if noneap_past~=.
recode labormarket_expectations_current .=1 if noneap_current~=. & unemployed_current~=.

 
eststo clear
	*TABLE 5
*Model 3.1
eststo: ologit health_ord_op i.labormarket_expectations_current gets_bennies_spouse spouse_not_formal noneap_current unemployed_current  lneduc lnage woman 
*Model 3.2
eststo: ologit health_ord_op i.labormarket_expectations_past gets_bennies_spouse spouse_not_formal noneap_past lneduc lnage woman 
*Model 3.3
eststo: ologit pension_ord_op i.labormarket_expectations_current gets_bennies_spouse spouse_not_formal noneap_current unemployed_current lneduc lnage woman 
*Model 3.4
eststo: ologit pension_ord_op i.labormarket_expectations_past gets_bennies_spouse spouse_not_formal noneap_past lneduc lnage woman 
*Model 3.5
eststo: reg contrib_v_noncontrib i.labormarket_expectations_current gets_bennies_spouse spouse_not_formal noneap_current unemployed_current lneduc lnage woman
*Model 3.6
eststo: reg contrib_v_noncontrib i.labormarket_expectations_past gets_bennies_spouse spouse_not_formal noneap_past lneduc lnage woman

esttab est1 est2 est3 est4 est5 est6 using Table3.rtf, label b(3) replace se star(+ 0.10 * 0.05 ) nogap onecell title(Table 2: The Correlates of Pension Attitudes: Mexico 2017)

margins, at(labormarket_expectations_past =1 gets_bennies_spouse=0 noneap_past=0) atmeans 
margins, at(labormarket_expectations_past =2 gets_bennies_spouse=0 noneap_past=0) atmeans 
margins, at(labormarket_expectations_past =3 gets_bennies_spouse=0 noneap_past=0) atmeans 
margins, at(labormarket_expectations_past =4 gets_bennies_spouse=0 noneap_past=0) atmeans 
margins, at(labormarket_expectations_past =1 gets_bennies_spouse=0 noneap_past=1) atmeans 
margins, at(labormarket_expectations_past =5 gets_bennies_spouse=0 noneap_past=0) atmeans 
margins, at(labormarket_expectations_past =6 gets_bennies_spouse=0 noneap_past=0) atmeans 
margins, at(labormarket_expectations_past =7 gets_bennies_spouse=0 noneap_past=0) atmeans 
margins, at(labormarket_expectations_past =8 gets_bennies_spouse=0 noneap_past=0) atmeans 

margins, at(labormarket_expectations_past =1 gets_bennies_spouse=1 noneap_past=1) atmeans 
margins, at(labormarket_expectations_past =5 gets_bennies_spouse=1 noneap_past=0) atmeans 
margins, at(labormarket_expectations_past =6 gets_bennies_spouse=1 noneap_past=0) atmeans 
margins, at(labormarket_expectations_past =7 gets_bennies_spouse=1 noneap_past=0) atmeans 
margins, at(labormarket_expectations_past =8 gets_bennies_spouse=1 noneap_past=0) atmeans 


margins, at(noneap_past=0 gets_bennies_spouse=0 labormarket_expectations_past=(1(1)4)) at(noneap_past=1 gets_bennies_spouse=0 labormarket_expectations_past=1) at(noneap_past=0 gets_bennies_spouse=0 labormarket_expectations_past=(5(1)8)) at(noneap_past=1 gets_bennies_spouse=1 labormarket_expectations_past=1) at(noneap_past=0 gets_bennies_spouse=1 labormarket_expectations_past=(5(1)8))  atmeans post
mat btemp=e(b)
mat b=btemp'
mat bvartemp=e(V)
mat bvartemp2=vecdiag(bvartemp)
mat bvar=bvartemp2'
mat list b
mat list bvar

svmat b
svmat bvar

gen hi=b1+1.9645*(bvar1^.5)
gen lo=b1-1.9645*(bvar1^.5)

gen n=_n if _n<5
gen n2=_n-.1 if _n>4 & _n<10
gen n3=_n+.1-5 if _n>9 & _n<15
gen n4=_n if _n<10

twoway (rcap hi lo n, lcolor(black)) (scatter b1 n, mcolor(black) connect(l) lcolor(black) sort msym(S)) ///
(rcap hi lo n2, lcolor(black)) (scatter b1 n2, mcolor(black) connect(l) lcolor(black) sort) ///
(rcap hi lo n3, lcolor(gs12)) (scatter b1 n3, mcolor(gs12) connect(l) lcolor(gs12) sort), /// 
ytitle("{bf:Index of Support for Noncontributory Programs}" "(in standard deviations)") ///
yscale(range(-.5 .5)) ylab(-.75 -.5 -.25 0 .25 .5) xscale(range(.75 9.25)) xline(4.5, lcolor(gs12) lwidth(thin)) ///
xline(5.5, lcolor(gs12) lwidth(thin)) /// 
xlab(2.5 "{bf:Prob. of transition to informality}" 7.5 "{bf:Prob. of transition to formality}", notick labsize(small) labgap(4)) ///
xmlab(1 "Zero" 2 "Low" 3 "Medium" 4 "High" 6 "High" 7 "Medium" 8 "Low" 9 "Zero", labsize(vsmall)) ///
  legend(order(2 "Formal coverage through self" 4 "No formal coverage" 6 "Formal coverage through spouse ") holes(3) span bmargin(tiny))  xtitle("") ///
 xtitle("") graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) ///
 text(.43 2.5 "Formal Workers") text(.43 7.5 "Informal Workers")  text(.43 5.02 "Never" "EAP", j(center)) 

 
	*FIGURE 3
twoway (rcap hi lo n, lcolor(black)) (scatter b1 n, mcolor(black) sort mcolor(black)) ///
(rcap hi lo n4, lcolor(black)) (scatter b1 n4, mcolor(black) sort), /// 
ytitle("{bf:Index of Support for Noncontributory Programs}" "(in standard deviations)") ///
yscale(range(-.5 .5)) ylab(-.75 -.5 -.25 0 .25 .5) xscale(range(.75 9.25)) xline(4.5, lcolor(gs12) lwidth(thin)) ///
xline(5.5, lcolor(gs12) lwidth(thin)) /// 
xlab(2.5 "{bf:Prob. of transition to informality}" 7.5 "{bf:Prob. of transition to formality}", notick labsize(small) labgap(4)) ///
xmlab(1 "Zero" 2 "Low" 3 "Medium" 4 "High" 6 "High" 7 "Medium" 8 "Low" 9 "Zero", labsize(vsmall)) ///
  legend(off) xtitle("") ///
 xtitle("") graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) ///
 text(.47 2.5 "Formal Workers") text(.47 7.5 "Informal Workers")  text(.45 5.02 "Never" "EAP", j(center)) 
 

*Playing with spousal stuff, per reviewer request
	*This is akin to a triple interaction, although splitting the sample to make it more interpretable
eststo: reg contrib_v_noncontrib i.labormarket_expectations_past noneap_past lneduc lnage woman if q_167==1
margins, at(labormarket_expectations_past =1 noneap_past=0) atmeans 
margins, at(labormarket_expectations_past =2 noneap_past=0) atmeans 
margins, at(labormarket_expectations_past =3 noneap_past=0) atmeans 
margins, at(labormarket_expectations_past =4 noneap_past=0) atmeans 
margins, at(labormarket_expectations_past =1 noneap_past=1) atmeans 
margins, at(labormarket_expectations_past =5 noneap_past=0) atmeans 
margins, at(labormarket_expectations_past =6 noneap_past=0) atmeans 
margins, at(labormarket_expectations_past =7 noneap_past=0) atmeans 
margins, at(labormarket_expectations_past =8 noneap_past=0) atmeans 

eststo: reg contrib_v_noncontrib i.labormarket_expectations_past noneap_past lneduc lnage woman if q_167~=1
margins, at(labormarket_expectations_past =1 noneap_past=0) atmeans 
margins, at(labormarket_expectations_past =2 noneap_past=0) atmeans 
margins, at(labormarket_expectations_past =3 noneap_past=0) atmeans 
margins, at(labormarket_expectations_past =4 noneap_past=0) atmeans 
margins, at(labormarket_expectations_past =1 noneap_past=1) atmeans 
margins, at(labormarket_expectations_past =5 noneap_past=0) atmeans 
margins, at(labormarket_expectations_past =6 noneap_past=0) atmeans 
margins, at(labormarket_expectations_past =7 noneap_past=0) atmeans 
margins, at(labormarket_expectations_past =8 noneap_past=0) atmeans 

	*and this is the simpler models reported in the text
recode labormarket_current (8=0) (else=1), gen(nonformal)
*recode labormarket_past (8=0) (else=1), gen(nonformal)

	*TABLE OA.5
ologit health_ord_op i.nonformal##i.spouse_formal lneduc lnage woman if q_167~=5
ologit pension_ord_op i.nonformal##i.spouse_formal lneduc lnage woman if q_167~=5


reg contrib_v_noncontrib i.nonformal##i.spouse_formal lneduc lnage woman if q_167~=5
	*TABLE 6
margins, at(nonformal=1 spouse_formal=0) atmeans 
margins, at(nonformal=1 spouse_formal=1) atmeans 
margins, at(nonformal=0 spouse_formal=0) atmeans 
margins, at(nonformal=0 spouse_formal=1) atmeans 
